/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Constraints.Impl;

import Constraints.IConstraint;
import Core.ErrorMessage;
import Model.Core.IMachineStateElement;
import Model.Diagrams.CommunicationDiagram;
import Model.Diagrams.StateMachine;
import Model.Draw.IMachineStateDraw;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Mih
 */
public class StateMachineDiagramConstraint implements IConstraint {
    @Override
    public List<ErrorMessage> Validate(Object obj) {
        List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
        Object[] elements = (Object[])obj;
        
        StateMachine diagram = (StateMachine)elements[0];
        IMachineStateDraw currentDraw = ((IMachineStateDraw)elements[1]);
        String name = (String)elements[2];
        
        for(IMachineStateElement entity : diagram.getStatesElements()){
            IMachineStateDraw draw = entity.getDraw();
            if(draw.getClass() == currentDraw.getClass() && !entity.equals(currentDraw)){
                if(draw.getDescription().equalsIgnoreCase(name)){
                    errors.add(new ErrorMessage("Rename", "O nome digitado já existe. Certifique-se que os nomes são únicos."));
                }
            }
        }
        return errors.size() > 0 ? errors : null;
    }
    
    
}
